package zdsmj;



import java.util.Scanner;

public class DrinkMachine {
    private static final int size=5;
    private static Goods[] gs = new Goods[size];
    private static int goodsIndex = 0;
    private static User[] users = new User[5];
    private static int userIndex = 0;
    private static VIPManager[] vips=new VIPManager[5];
    private static int vipIndex=1;
    private static User u;
    private static boolean flag=true;
    private static int id;
    private static int pwd;
    private static int[] goods_id=new int[5];
    private static int state=0;//1多种购买 2单种购买;
    private static int cardBlance;//会员卡余额
    //会员信息
   private static int vip_index=5;
    //主界面
    private static void menu(){
        System.out.println("=============欢迎使用饮料售卖机================");
        System.out.println("\t1--直接购买商品");
        System.out.println("\t2--商家登陆");
        System.out.println("\t3--会员管理");
        System.out.println("\t0--退出系统");
        System.out.println("===============================================");

    }
    //选择饮料
    private  static int goodsChoose() {
        Scanner sc1=new Scanner(System.in);
        while (flag) {
            System.out.println("请选择饮品：(0--退出)");
            id = sc1.nextInt();
            if(id!=0) {
                if (gs[id - 1] != null&&id<=gs.length) {
                    System.out.println("您选择的是：" + gs[id - 1].getName());
                    gs[id-1].setNum(gs[id-1].getNum()-1);
                    flag = false;
                } else {
                    System.out.println("无此饮料，请重新选择：(0--退出)");
                }


            }else{
                break;
            }

        }

        return id;

    }

    //饮料多选
    private static int[] chooseMore(){
        Scanner sc12=new Scanner(System.in);
        boolean is=true;
        while (is) {
            for (int x = 0; x < goods_id.length; x++) {
                System.out.println("请选择饮品：（0-退出）");
                goods_id[x] = sc12.nextInt();
                if (goods_id[x] != 0) {
                    if (gs[goods_id[x] - 1] != null && goods_id[x] < gs.length) {
                        System.out.println("您选择的是：" + gs[goods_id[x] - 1].getName());
                        gs[goods_id[x] -1].setNum(gs[goods_id[x] -1].getNum()-1);
                        is = false;
                    } else {
                        System.out.println("无此饮料，请重新选择：(0--退出)");
                    }
                } else {
                    break;
                }
            }

        }
        return goods_id;
    }

    //饮料多合付钱
    private static void payMore(){

        int price=0;
        for (int x = 0; x <goods_id.length ; x++) {
            if(goods_id[x]!=0) {
                price = price + gs[goods_id[x] - 1].getPrice();
            }else if(goods_id[x]==0){
                price=price+0;
            }
        }
        System.out.println("总计"+price+"元");
        System.out.println("请按规定的现金支付");
        sMoney();
        MoneyCounter mc=new MoneyCounter();
        mc.Cal(price,cardBlance);
    }

    //购买多种饮料
    private static int good_Chooses(){
            System.out.println("是否选择多种饮品 （是-1）（否-2）(退出-0)");
            Scanner sc=new Scanner(System.in);
            state= sc.nextInt();
            switch (state){
                case 1:
                try{
                    for (int x = 0; x <gs.length; x++) {
                        System.out.println(gs[x].toString());
                    }
                }catch (Exception e){
                }
                try{
                 chooseMore();
                }catch (Exception e){
                }
                if(goods_id!=null){
                    try{
                        cardPay();
                    }catch (Exception e){}
                    try{
                        payMore();
                    }catch (Exception e){
                    }
                }else {
                    System.out.println("bye-bye,欢迎下次购买...");
                }
                break;
            case 2:
                try{
                    for (int j = 0; j<gs.length; j++) {
                        System.out.println(gs[j].toString());
                    }
                }catch (Exception e){
                }

                try{
                    goodsChoose();
                }catch (Exception e){
                }

                if(id!=0){
                    try{
                        cardPay();
                    }catch (Exception e){}
                    try{
                        goodsPay();
                    }catch (Exception e){
                    }
                }else {
                    System.out.println("bye-bye,欢迎下次购买...");
                }
                break;
            case 0:
                break;
                default:
                    System.out.println("错误操作码,请重新输入：");
        }


        return state;
    }

    //规定现金
    private static void sMoney(){
        System.out.println("1.RMB5元");
        System.out.println("2.RMB10元");
        System.out.println("3.RMB20元");
        System.out.println("4.RMB50元");
        System.out.println("5.RMB100元");
    }
    //请按规定现金支付
    private static void goodsPay(){
        System.out.println("请按规定的现金支付");
        sMoney();
        MoneyCounter mc=new MoneyCounter();
        mc.Cal(gs[id-1].getPrice(),cardBlance);


    }

    //会员卡结算
    private static int cardPay() {

        //判断是否登陆会员
        MoneyCounter mc = new MoneyCounter();
        if (vip_index == 5) {
            System.out.println("如果选择会员卡支付，请先登陆会员");
        } else if (vip_index < 5) {
            cardBlance=vips[vip_index].getBlance();
            System.out.println("会员卡余额" + cardBlance);
            Scanner s1 = new Scanner(System.in);
            System.out.println("是否选择会员卡支付:(1--是)（2--否）");
            int op = s1.nextInt();
            if (op == 1) {
                mc.setTotal(cardBlance);
            }else if(op==2){
                cardBlance=0;
            }

        }
        return cardBlance;


    }

    //商家登陆
     private static void login(){
        Scanner sc2=new Scanner(System.in);
        String user_name;
        int password;
        int count=3;
        while (count!=0){
                System.out.println("请输入登陆用户名：");
                user_name=sc2.next();
                System.out.println("请输入密码：");
                password=sc2.nextInt();
                    for (userIndex = 0; userIndex < users.length; userIndex++) {
                        if (user_name.equals(users[userIndex].getUsername())&&password==users[userIndex].getPassword()){
                        pwd=password;
                        System.out.println("欢迎" + user_name + "登陆成功");
                        count = 0;
                        managerOption();
                        }else {
                count--;
                System.out.println("账号密码错误，请重新输入：");
                System.out.println("还剩"+count+"次机会");
                break;
            }
        }
        }





    }

    //商家管理界面
    private static void managerMenu(){
        System.out.println("=================商家管理界面=====================");
        System.out.println("\t1.查询饮料");
        System.out.println("\t2.增加饮料种类");
        System.out.println("\t3.补充饮料库存");
        System.out.println("\t4.修改密码");
        System.out.println("\t0.返回上一级");
        System.out.println("==================================================");
    }

    //商家操作
    private static void managerOption(){
        Scanner sc4=new Scanner(System.in);
        int op;
        do {
            managerMenu();
            System.out.println("请选择操作码：");
            op=sc4.nextInt();
            switch (op){
                case 1:
                    try{
                        for (int i = 0; i <gs.length; i++) {
                            System.out.println(gs[i].toString());
                        }
                    }catch (Exception e){
                    }
                    break;
                case 2:
                    incraseDrink();
                    break;
                case 3:
                    try{
                        for (int i = 0; i <gs.length; i++) {
                            System.out.println(gs[i].toString());
                        }
                    }catch (Exception e){
                    }
                    addDrinkNum();
                    break;
                case 4:
                    changePassword();
                    break;
                case 0:
                    break;
                default:
                    System.out.println("错误操作码，请重新输入：");
            }

        }while (op!=0);
    }

    //增加饮料种类
    private static void incraseDrink(){
        int id;
        int op;
        String name;
        int price;
        int num;
        int jifen;
        boolean testflag=true;
        Scanner sc3=new Scanner(System.in);
        while (testflag){
            System.out.println("是否添加：（是--1）（否--0）");
            op=sc3.nextInt();
            if(op==1){
                System.out.println("请输入编号：");
                id=sc3.nextInt();
                System.out.println("请输入名称：");
                name=sc3.next();
                System.out.println("请输入价格:");
                price=sc3.nextInt();
                System.out.println("请输入数量:");
                num=sc3.nextInt();
                System.out.println("请输入会员积分：");
                jifen=sc3.nextInt();

                Goods g=new Goods(id,name,price,num,jifen);
                for(goodsIndex=0;goodsIndex<gs.length;goodsIndex++){
                    if (gs[goodsIndex].getName()!=name&&gs[goodsIndex].getId()!=id){
                        gs[id-1]=g;
                        System.out.println("添加饮料成功！当前饮料售卖机容量为："+gs.length);
                        testflag=false;
                        break;
                    }else {
                        System.out.println("添加饮料失败！请勿重复添加商品\n重新添加");
                        break;
                    }
                }
            }else if(op==0){
                testflag=false;
                break;

            }




        }






    }

    //补充饮料库存
    private static void addDrinkNum(){
        boolean f=true;
        int goodId;
        Scanner sc4=new Scanner(System.in);
        while (f) {
            System.out.println("请选择补充库存的饮品：(0--退出)");
            goodId=sc4.nextInt();
            if(goodId!=0) {
                if (gs[goodId - 1] != null && goodId < gs.length) {
                    System.out.println("您选择的是：" + gs[goodId-1].getName());
                    f = false;
                    System.out.println("请输入补充库存的数量：");
                    int num=sc4.nextInt();
                    gs[goodId-1].setNum(gs[goodId-1].getNum()+num);
                    System.out.println("补充库存成功");
                    System.out.println("商品"+gs[goodId-1].getName()+"的数量是"+gs[goodId-1].getNum());
                    break;
                } else {
                    System.out.println("无此饮料，请重新选择：(0--退出)");
                }
            }else {
                break;
            }

        }







    }

    //修改密码
    private static void  changePassword(){
        Scanner sc5=new Scanner(System.in);
        int oldPwd;
        int newPwd;
        int j=3;
        while (j!=0){
            System.out.println("请输入当前密码：");
            oldPwd=sc5.nextInt();
            if(oldPwd!=pwd){
                j--;
                System.out.println("当前密码错误，剩余"+j+"次机会");

            }else {
                j=0;
                System.out.println("请输入更改密码：");
                newPwd=sc5.nextInt();
                pwd=newPwd;
                System.out.println("密码修改成功");
                break;

            }
        }



    }

    //会员管理
    private static void vipManager(){
        Scanner sc6=new Scanner(System.in);
        int op;
        do {
            vipManagerMenu();
            System.out.println("请输入操作码：");
            op=sc6.nextInt();
            switch (op){
                case 1:
                    try{
                        registrationVip();
                    }catch (Exception e){
                    }
                    break;
                case 2:
                    try{
                        vipLogin();
                    }catch (Exception e){
                    }
                    break;
                case 0:
                    break;
                  default:
                      System.out.println("错误操作码，请重新输入");
            }
        }while (op!=0);

    }

    //vip管理界面
    private static void vipManagerMenu(){
        System.out.println("==================VIP管理界面==================");
        System.out.println("\t1.注册会员");
        System.out.println("\t2.会员登陆");
        System.out.println("\t0.返回");
        System.out.println("===============================================");
    }

    //vip操作界面
    private static void vipOptionMenu(){
        System.out.println("===================VIP操作界面==================");
        System.out.println("\t1.信息查询");
        System.out.println("\t2.会员充值");
        System.out.println("\t3.更新信息");
        System.out.println("\t4.会员积分");
        System.out.println("\t0.返回");
        System.out.println("===============================================");

    }

    //vip注册
    private static int registrationVip(){
        Scanner sc6=new Scanner(System.in);
        String vip_name;
        int vip_id;
        int vip_password;
        int again_pwd;
        boolean istrue=true;
        VIPManager vipManager;
        while (istrue){
            System.out.println("请输入账号id：");
            vip_id=sc6.nextInt();
            System.out.println("请输入账号名称：");
            vip_name=sc6.next();
            System.out.println("请设置账号密码：");
            vip_password=sc6.nextInt();
            System.out.println("请再次确认密码：");
            again_pwd=sc6.nextInt();
            for (int i = 0; i <vips.length ; i++) {
                if(vip_id==vips[i].getVip_id()){
                    System.out.println("该账号id已经存在，请重新输入");
                    break;
                }else if(again_pwd==vip_password){
                    vipManager=new VIPManager(vip_name,vip_id,vip_password,0,0);
                    vips[vipIndex]=vipManager;
                    vipIndex++;
                    System.out.println("会员注册成功");
                    istrue=false;
                    break;
                }

            }

        }
        return vipIndex;



    }

    //vip登陆
    private  static  void vipLogin(){
        Scanner sc7 = new Scanner(System.in);
        int userid_vip;
        int pwd_vip;
            System.out.println("请输入账号id：");
            userid_vip = sc7.nextInt();
            System.out.println("请输入账号密码：");
            pwd_vip = sc7.nextInt();
            int id;
            int pwd;
            String name;
            for (int i = 0; i <vips.length; i++) {
               id= vips[i].getVip_id();
               pwd = vips[i].getVip_pwd();
               name= vips[i].getVip_name();
               if (id == userid_vip && pwd == pwd_vip) {
                    vip_index = i;
                    System.out.println("尊贵的vip" + name + "登陆成功");
                    vipOption();
               }
            }
        }
    //vip操作
    private static  void vipOption(){
        Scanner sc8=new Scanner(System.in);
        int op;
        do {
            vipOptionMenu();
            System.out.println("请输入操作码：");
            op=sc8.nextInt();
            switch (op){
                case 1:
                    try{
                        System.out.println(vips[vip_index]);
                    }catch (Exception e){
                    }
                    break;
                case 2:
                    recharge();
                    break;
                case 3:
                    renewId();
                    break;
                case 4:
                     jifen();
                    break;
                case 0:
                    break;
                default:
                    System.out.println("错误操作码，请重新输入");
            }
        }while (op!=0);
    }
    //vip更新信息
    private static void renewId(){
        Scanner sc9=new Scanner(System.in);
        String vip_name;
        int vip_password;
        VIPManager vipManager;
        System.out.println("请输入账号名称：");
        vip_name=sc9.next();
        System.out.println("请设置账号密码：");
        vip_password=sc9.nextInt();
        vipManager=new VIPManager(vip_name,vips[vip_index].getVip_id(),vip_password,vips[vip_index].getIntegral(),vips[vip_index].getBlance());
        vips[vip_index]=vipManager;
        System.out.println("信息更新成功");
                }
    //vip会员卡充值
    private static void recharge(){
        Scanner sc10=new Scanner(System.in);
        System.out.println("请按规定面值输入要充值的金额");
        sMoney();
        System.out.println("请输入...");
        int Blance=sc10.nextInt();
         vips[vip_index].setBlance(vips[vip_index].getBlance()+Blance);
         System.out.println("充值成功");
    }
    //vip会员积分
    private static void jifen(){

        if(state==2){
            vips[vip_index].setIntegral(vips[vip_index].getIntegral()+gs[id-1].getJifen());
            System.out.println("购买商品所加积分"+gs[id-1].getJifen());
        }else if(state==1){
            int jf=0;
            for (int x=0;x<goods_id.length;x++){
                if(goods_id[x]!=0) {
                    jf = jf + gs[goods_id[x] - 1].getJifen();
                }else if(goods_id[x]==0){
                    jf=jf+0;
                }
            }
            vips[vip_index].setIntegral(vips[vip_index].getIntegral()+jf);
            System.out.println("购买商品所加积分"+jf);
        }

        System.out.println("会员总积分："+vips[vip_index].getIntegral());
    }


    static { //初始化饮料和用户
        Goods goods1=new Goods(1,"可乐",10,20,5);
        gs[0]=goods1;
        Goods goods2=new Goods(2,"冰红茶",3,12,3);
        gs[1]=goods2;
        Goods goods3=new Goods(3,"橙汁",8,10,4);
        gs[2]=goods3;


        User user1=new User("aa",11);
        users[0]=user1;

        VIPManager vipManager=new VIPManager("妍",12,1234,111,12);
        vips[0]=vipManager;







    }
    public static void main(String[] args) {
            int op;
            do {
                menu();
                System.out.println("请选择操作代码：");
                Scanner sc=new Scanner(System.in);
                op =sc.nextInt();
                switch (op){
                    case 1:
                        good_Chooses();
                        break;
                    case 2:
                        try{login();}catch (Exception e){}
                        break;
                    case 3:
                        vipManager();
                        break;
                    case 0:
                        System.out.println("欢迎下次使用");
                        break;
                    default:
                        System.out.println("输入操作有误，请重新输入");
                }
            }while (op!=0);


    }

}
